//
//  SystemSetingCell.m
//  PigMarket
//
//  Created by mac on 2019/3/20.
//  Copyright © 2019 felix.pigmarket.oc. All rights reserved.
//

#import "SystemSetingCell.h"
#import "GroupSetUpHeaderView.h"

@interface SystemSetingCell ()

/**
 titleLab
 */
@property (nonatomic , strong) UILabel *titleLab;
/**
 群聊天设置 头像信息
 */
@property (nonatomic , strong) GroupSetUpHeaderView *headImgView;

@end

@implementation SystemSetingCell
#pragma mark - init method
+ (instancetype)cellWithTableView:(UITableView *)tableView{
    static NSString *identifierID = @"identifierID";
    SystemSetingCell *cell = [tableView dequeueReusableCellWithIdentifier:identifierID];
    if (cell == nil) {
        cell = [[SystemSetingCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifierID];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self initView];
        [self initLayout];
    }
    return self;
}
- (void)initView {
    // 添加子控件
    [self.contentView addSubview:self.titleLab];
    [self.contentView addSubview:self.detailLab];
    [self.contentView addSubview:self.mySwitch];
    [self.contentView addSubview:self.headImgView];
}

- (void)initLayout {
    // 布局子控件
    [self.titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.centerY.mas_equalTo(self.contentView.mas_centerY);
        make.top.bottom.mas_equalTo(0);
    }];
    [self.detailLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(0);
        make.left.mas_equalTo(self.titleLab.mas_right).offset(10);
        make.top.bottom.mas_equalTo(0);
        make.centerY.mas_equalTo(self.titleLab.mas_centerY);
    }];
    [self.mySwitch mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-15);
        make.centerY.mas_equalTo(self.titleLab.mas_centerY);
    }];
    [self.headImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.mas_equalTo(0);
    }];
}
#pragma mark --------------------- 单聊设置赋值 ------------------------
- (void)configChatCellWithDataDic:(NSDictionary *)dataDic indexPath:(NSIndexPath *)indexPath{
    self.titleLab.text = dataDic[@"title"];
    if (indexPath.section == 1 && indexPath.row == 0) {
        self.mySwitch.hidden = NO;
        if ([dataDic[@"detail"] boolValue]) {
            self.mySwitch.on = YES;
        } else {
            self.mySwitch.on = NO;
        }
    } else {
        self.mySwitch.hidden = YES;
        self.detailLab.text = dataDic[@"detail"];
    }
}
- (void)mySwitch:(UISwitch *)mymySwitch {
    if (self.mySwitchBlock) {
        self.mySwitchBlock(mymySwitch.on);
    }
}
#pragma mark --------------------- 群聊消息设置 ------------------------
- (void)configGroupCellWithGroupSetUpDic:(NSDictionary *)groupSetUpDic headInfo:(NSMutableArray *)headInfo {
    self.detailLab.text = groupSetUpDic[@"detail"];
    if ([groupSetUpDic[@"title"] isEqualToString:@"headerInfo"]) {
        //第一个cell，头像信息
        self.headImgView.hidden = NO;
        self.headImgView.dataArr = headInfo;
    } else {
        self.headImgView.hidden = YES;
        self.titleLab.text = groupSetUpDic[@"title"];
    }
}
#pragma mark --------------------- 群管理 ------------------------
- (void)configGroupManagerCellWithDataDic:(NSDictionary *)dataDic indexPath:(NSIndexPath *)indexPath {
    self.titleLab.text = dataDic[@"title"];
    if (indexPath.section == 0 && indexPath.row == 0) {
        self.mySwitch.hidden = NO;
        if ([dataDic[@"detail"] boolValue]) {
            self.mySwitch.on = YES;
        } else {
            self.mySwitch.on = NO;
        }
    } else {
        self.mySwitch.hidden = YES;
        self.detailLab.text = dataDic[@"detail"];
    }
}

#pragma mark - getter and setter

//系统设置
- (void)setInfoDic:(NSDictionary *)infoDic {
    _infoDic = infoDic;
    self.titleLab.text = infoDic[@"title"];
    self.detailLab.text = infoDic[@"detail"];
}
- (UILabel *)titleLab {
    if (!_titleLab) {
        _titleLab = [[UILabel alloc] init];
        _titleLab.textColor = [UIColor textBlackColor];
        _titleLab.font = [UIFont largerFont];
    }
    return _titleLab;
}
- (UILabel *)detailLab {
    if (!_detailLab) {
        _detailLab = [[UILabel alloc] init];
        _detailLab.textColor = [UIColor colorWithHexString:@"#999999"];
        _detailLab.font = [UIFont largerFont];
        _detailLab.textAlignment = NSTextAlignmentRight;
    }
    return _detailLab;
}
- (UISwitch *)mySwitch {
    if (!_mySwitch) {
        _mySwitch = [[UISwitch alloc] init];
        _mySwitch.hidden = YES;
        [_mySwitch addTarget:self action:@selector(mySwitch:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _mySwitch;
}
- (GroupSetUpHeaderView *)headImgView {
    if(!_headImgView) {
        _headImgView = [[GroupSetUpHeaderView alloc] init];
        _headImgView.backgroundColor = [UIColor whiteColor];
        _headImgView.hidden = YES;
    }
    return _headImgView;
}
@end

#pragma mark ------------ChangeAccountCell------------------------------------------------------

@interface ChangeAccountCell ()
/**
 title
 */
@property (nonatomic , strong) UILabel *accountLab;

/**
 img
 */
@property (nonatomic , strong) UIImageView *headImg;


@end

@implementation ChangeAccountCell
+ (instancetype)cellWithTableView:(UITableView *)tableView {
    static NSString *identifierID = @"identifierID";
    ChangeAccountCell *cell = [tableView dequeueReusableCellWithIdentifier:identifierID];
    if (cell == nil) {
        cell = [[ChangeAccountCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifierID];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self initView];
        [self initLayout];
    }
    return self;
}

- (void)initView {
    // 添加子控件
    [self.contentView addSubview:self.headImg];
    [self.contentView addSubview:self.accountLab];
    [self.contentView addSubview:self.selectImg];
}

- (void)initLayout {
    // 布局子控件
    [self.headImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.size.mas_equalTo(CGSizeMake(32, 32));
        make.centerY.mas_equalTo(self.contentView.mas_centerY);
    }];
    [self.accountLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.headImg.mas_centerY);
        make.left.mas_equalTo(self.headImg.mas_right).offset(15);
        make.top.bottom.mas_equalTo(0);
    }];
    [self.selectImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-22);
        make.centerY.mas_equalTo(self.headImg.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(18, 18));
    }];
    
}

#pragma mark - setter mehtods

- (void)setItemModel:(LoginItemModel *)itemModel{
    
    [_headImg sd_setImageWithURL:[NSURL URLWithString:itemModel.headImg] placeholderImage:[UIImage imageNamed:@"placeholder_header"]];
    _accountLab.text = itemModel.Phone;
    _selectImg.hidden = !itemModel.selected;
}

#pragma mark - getter mehtods

- (UIImageView *)headImg {
    if (!_headImg) {
        _headImg = [[UIImageView alloc] init];
        [self addShadowAndCircleCorner:_headImg.layer corner:16];
    }
    return _headImg;
}
- (UILabel *)accountLab {
    if (!_accountLab) {
        _accountLab = [[UILabel alloc] init];
        _accountLab.textColor = [UIColor textBlackColor];
        _accountLab.font = [UIFont largerFont];
    }
    return _accountLab;
}

- (UIImageView *)selectImg {
    if (!_selectImg) {
        _selectImg  = [[UIImageView alloc] init];
        _selectImg.image = [UIImage imageNamed:@"mine_account_selected"];
        _selectImg.hidden = YES;
    }
    return _selectImg;
}

@end
